package com.liuyfly.commons.utils;

import com.liuyfly.datastructure.dto.Node;
import com.liuyfly.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author liuyangfang 2021/8/23 上午1:52
 */
@Slf4j
public class CommonUtils {

    private static char[] baseCharacters = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

    public static String generateMobile() {
        return "1580000" + generateRandomNumber(1000, 10000);
    }

    public static String generateName() {
        String first = generateRandomString(1).toUpperCase();
        String firstName = first + generateRandomString(6);
        String last = generateRandomString(1).toUpperCase();
        String lastName = last + generateRandomString(6);
        return firstName + " " + lastName;
    }

    public static String generateRandomString(int length) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append(baseCharacters[generateRandomNumber(baseCharacters.length)]);
        }
        return builder.toString();
    }

    public static void printArray(Object[] array) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            builder.append(JsonUtil.toJson(array[i]));
            if (i < array.length - 1) {
                builder.append(", ");
            }
        }
        log.info("array:[{}]", builder.toString());
    }

    public static <T extends Comparable<T>> void printList(List<T> list) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            builder.append(JsonUtil.toJson(list.get(i)));
            if (i < list.size() - 1) {
                builder.append(", ");
            }
        }
        log.info("list:[{}]", builder.toString());
    }

    public static <T extends Comparable<T>> void printNodeList(Node<T> node) {
        Node<?> temp = node;
        while (temp != null){
            System.out.println("node value:"+ JsonUtil.toJson(temp.getValue()));
            temp = temp.getNext();
        }
    }

    public static Integer generateRandomNumber(int min, int max) {
        Random random = new Random();
        return Math.abs(random.nextInt()) % (max - min) + min;
    }

    public static Integer generateRandomNumber(int max) {
        Random random = new Random();
        return Math.abs(random.nextInt()) % max;
    }

    public static Integer[] generateRandomArray(int length, int max) {
        Integer[] array = new Integer[length];
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            array[i] = Math.abs(random.nextInt()) % max;
        }
        return array;
    }

    public static List<Integer> generateRandomList(int length, int max) {
        List<Integer> list = new ArrayList<>(length);
        for (int i = 0; i < length; i++) {
            list.add(generateRandomNumber(max));
        }
        return list;
    }

    public static Node<Integer> generateRandomNodeList(int length, int max) {
        Node<Integer> head = new Node<>(generateRandomNumber(max));
        if (length==1){
            return head;
        }
        head.setNext(generateRandomNodeList(length-1, max));
        return head;
    }
}
